package com.example.utils;


/**
 * 基本类型转换
 * @author 李隆铨
 *
 */
public class TypeUtils {
	  @SuppressWarnings("unchecked")
	    public static <T> T convert(Object object, Class<T> targetType) throws Exception {
	        T targetObject = null;
	        if (object == null) {
	            throw new Exception("object cat't be null");
	        } else if (targetType == null) {
	            throw new Exception("targetType cat't be null");
	        } else if (object instanceof String) {
	            if (String.class.equals(targetType)) {
	                targetObject = (T) object;
	            } else if (Long.class.equals(targetType)) {
	                targetObject = (T) Long.valueOf((String) object);
	            } else if (Integer.class.equals(targetObject)) {
	                targetObject = (T) Long.valueOf((String) object);
	            } else if (Boolean.class.equals(targetObject)) {
	                targetObject = (T) Long.valueOf((String) object);
	            }
	        } else if (object instanceof Long) {
	            if (String.class.equals(targetType)) {
	                targetObject = (T) String.valueOf(object);
	            } else if (Long.class.equals(targetType)) {
	                targetObject = (T) object;
	            } else if (Integer.class.equals(targetType)) {
	                targetObject = (T) Integer.valueOf(String.valueOf(targetType));
	            } else if (Boolean.class.equals(targetObject)) {
	                targetObject = (T) Integer.valueOf(String.valueOf(targetType));
	            } else {
	                targetObject = (T) object;
	            }	        	            
	        }
	        
	        return targetObject;
	    }
}
